import {testNetWork} from "./env"
import { ethers, providers, utils } from "ethers";
export async function sendETH() {
    const main = async () => {
      // 1. provider 实例
      const providerTest = new ethers.providers.JsonRpcProvider(testNetWork)
      // 2. 创建钱包实例
      // 2.1 创建随机钱包实例（新建） 单机
      const wallet1 = ethers.Wallet.createRandom()
      const walletProvider = wallet1.connect(providerTest)
      const walletKey = walletProvider.privateKey
      const balance = await walletProvider.getBalance()
      console.log('wallet1 发送前的钱包余额', balance.toNumber())
      // 2.2 通过私钥创建实例
      const wallet2Key = '5e34716a60a0926d67c629fae8752cdc9fbe742095a093d40e5b1a2834cc981e'
      const wallet2 = new ethers.Wallet(wallet2Key,providerTest)
      const wallet2Address = await wallet2.getAddress()
      const balance2 = await wallet2.getBalance()
      console.log('wallet2 发送前的钱包余额', ethers.utils.formatEther(balance2))

      // console.log('wallet2Address',wallet2Address)

      // 2.3 通过助记词来创建实例
      // console.log('walletProvider.mnemonic',walletProvider.mnemonic.phrase)
      // const mnemonic = walletProvider.mnemonic.phrase
      // const wallet3 = new ethers.Wallet.fromMnemonic(mnemonic)
      // console.log('wallet1 和 wallet3 地址是否一样', await walletProvider.getAddress() === await wallet3.getAddress())

      // 3 发送ETH 
      const wallet1Address = await walletProvider.getAddress()
      console.log('wallet1Address',wallet1Address)
      const tx = {
        to: wallet1Address,
        value: ethers.utils.parseEther('0.01')
      }

      const receipt = await wallet2.sendTransaction(tx)
      // 等待链上交易确认
      const result = await receipt.wait()
      console.log('result',result)
      const resultBalace = await walletProvider.getBalance()
      console.log('发送后 wallet1 的余额', ethers.utils.formatEther(resultBalace))

    }
    main()
}